package com.gmako.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.gmako.entity.Packageinfo;
import com.gmako.service.InfoManagerService;
import com.gmako.utils.Pager;

/**
 * 套餐/团购管理
 * 
 * 
 */
@Controller
@RequestMapping("/managerInfo.do")
public class InfomationManagerController extends BaseController {
	@Autowired(required = false)
	private InfoManagerService infoManagerService;
	
	/**
	 * 显示查询默认搜索
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=viewSearchPage")
	public ModelAndView selectStreetPhotoInfoView(HttpServletRequest request,
			HttpServletResponse response) {
		int pageNo = 1;
		int pageSize = 10;
		String title = "";
		Pager pager = infoManagerService.findPagerByHql(pageSize, pageNo,
				title);
		HashMap<Object,Object> map  = new HashMap<Object, Object>();
		List<HashMap<Object,Object>> list = pager.getResultList();
		for(int i =0;i<list.size();i++){
			System.out.println(list.get(i).get("PackageName"));
		}
		request.setAttribute("pager", pager);
		return new ModelAndView("admin/businessManager");
	}
	
	/**
	 * 显示查询条件搜索
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(params = "method=viewSearchPageByparameter")
	public ModelAndView selectStreetInfoViewByP(
			@RequestParam("title") String title, HttpServletRequest request,
			HttpServletResponse response) throws UnsupportedEncodingException {
		int pageNo = 1;
		int pageSize = 10;
		if (title != null && title != "") {
			title = URLDecoder.decode(title.toString(), "UTF-8").trim();
		}
		Pager pager = infoManagerService.findPagerByHql(pageSize, pageNo,
				title);
		request.setAttribute("pager", pager);
		return new ModelAndView("admin/businessManager");
		// return null;
	}
	
	/**
	 * 显示查询条件搜索分页
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(params = "method=viewSearchPager")
	public ModelAndView selectStreetInfoViewByPForPager(
			@RequestParam("title") String title,
			@RequestParam("pageNo") String r_pageNo,
			HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException {
		int pageNo = 1;
		int pageSize = 10;
		if (title != null && title != "") {
			title = URLDecoder.decode(title, "UTF-8").trim();
		}
		if (r_pageNo != null && r_pageNo != "") {
			pageNo = Integer.parseInt(r_pageNo.toString());
		}
		int type = Integer.parseInt(request.getParameter("type").toString());
		Pager pager = infoManagerService.findPagerByHql(pageSize, pageNo,
				title);
		request.setAttribute("pager", pager);
		return new ModelAndView("admin/businessManager");
	}
	
	
	/**
	 * 删除信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(params = "method=delete")
	public ModelAndView deleteStreetInfoView(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		infoManagerService.deleteInfo(Packageinfo.class, id);
		response.sendRedirect("/managerInfo.do?method=viewSearchPage");
		return null;
	}

}
